package com.ip.student.service;

import com.ip.student.models.bo.StudentAwardSaveBO;
import com.ip.student.models.dto.StudentAwardDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ip.student.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ip.student.mapper.ScoreCourseMapper;
import com.ip.student.models.service.IScoreCourseService;
import com.ip.student.models.bo.ScoreCourseSaveBO;
import com.ip.student.models.dto.ScoreCourseDTO;

/**
 * 课程信息控制器
 *
 * @author 张景奇
 * @Date 2025-11-06 10:15:15
 */
@DubboService
public class ScoreCourseServiceImpl extends ServiceImpl<ScoreCourseMapper, ScoreCourseDTO> implements IScoreCourseService{

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(ScoreCourseSaveBO saveBO) {
        ScoreCourseDTO dto = new ScoreCourseDTO();
        BeanUtils.copyProperties(saveBO,dto);

        // 如果前端传回的ID为null或0，则使用雪花算法生成ID
        if (saveBO.getCourseId() == null || saveBO.getCourseId() == 0) {
            Long id = ymlConfig.getPkId();
            // 确保生成的ID为正数且在Integer范围内
            if (id < 0) {
                id = Math.abs(id);
            }
            // 确保ID在Integer范围内
            if (id > Integer.MAX_VALUE) {
                id = id % Integer.MAX_VALUE;
            }
            // 确保ID不为0
            if (id == 0) {
                id = 1L;
            }
            dto.setCourseId(id.intValue());
        } else {
            dto.setCourseId(saveBO.getCourseId());
        }
        // 如果courseCode为空，则使用雪花算法生成编码
        if (saveBO.getCourseCode() == null || saveBO.getCourseCode().trim().isEmpty()) {
            Long id = ymlConfig.getPkId();
            // 确保生成的ID为正数
            if (id < 0) {
                id = Math.abs(id);
            }
            // 确保ID在Integer范围内
            if (id > Integer.MAX_VALUE) {
                id = id % Integer.MAX_VALUE;
            }
            // 确保ID不为0
            if (id == 0) {
                id = 1L;
            }
            dto.setCourseCode("Cou-" + id);
        } else {
            dto.setCourseCode(saveBO.getCourseCode());
        }

        return this.insert(dto);
    }
}
